    //
//  DescriptionViewController.m
//  RssReader
//
//  Created by Wang Shuguang on 12-5-11.
//  Copyright 2012 Dawn. All rights reserved.
//

#import "DescriptionViewController.h"
#import "Rss.h"


@implementation DescriptionViewController
@synthesize item;
@synthesize channelIndex;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.view.frame = CGRectMake(0, 20, 320, 460);
	self.view.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0];
	
	descWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 34, 320, 460-34-44)];
	
	descWebView.dataDetectorTypes = UIDataDetectorTypeNone;
	descWebView.backgroundColor = [UIColor clearColor];
	descWebView.opaque = NO;
	[descWebView setScrollShadowHidden:YES];
	descWebView.delegate = self;
	[self.view addSubview:descWebView];
	[descWebView release];
	
	
	//导航条
	navView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 37)];
	navView.image = [UIImage noCacheImageNamed:@"navbar_background.png"];
	navView.userInteractionEnabled = YES;
	[self.view addSubview:navView];
	//返回按钮
	UIButton *backButton = [PublicMethods buttonWithImage:nil 
										  backgroundImage:[UIImage noCacheImageNamed:@"navbar_back_normal.png"] 
							   highlightedBackgroundImage:[UIImage noCacheImageNamed:@"navbar_back_heighlight.png"]];
	backButton.frame = CGRectMake(0, 0, 47, 35);
	[backButton addTarget:self action:@selector(navBack:) forControlEvents:UIControlEventTouchUpInside];
	[navView addSubview:backButton];
	//频道名
	UILabel *navTitle = [[UILabel alloc] initWithFrame:CGRectMake(47, 0, 320-47-47, 37)];
	navTitle.font = [UIFont systemFontOfSize:12.0f];
	navTitle.textColor = [UIColor colorWithRed:100.0f/255 green:100.0f/255 blue:100.0f/255 alpha:1];
	navTitle.textAlignment = UITextAlignmentCenter;
	navTitle.backgroundColor = [UIColor clearColor];
	[navView addSubview:navTitle];
	[navTitle release];
	
	navTitle.text = [item objectForKey:@"title"];
	
	//向右滑动跳转回上一页
	UISwipeGestureRecognizer *oneFingerSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeRight:)];
	[oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
	[[self view] addGestureRecognizer:oneFingerSwipeRight];
	[oneFingerSwipeRight release];
	
	    
    
	moreButton = [PublicMethods buttonWithImage:nil backgroundImage:[UIImage imageNamed:@"more_item"] highlightedBackgroundImage:[UIImage imageNamed:@"more_item_heighlight.png"]];
    moreButton.frame = CGRectMake(0, 460-40, 304, 33);
    moreButton.center = CGPointMake(320/2, 33.0/2 + 460-40);
    [moreButton setTitle:@"显示更多" forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0/255.0f alpha:1] forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [moreButton addTarget:self action:@selector(newsDetail:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:moreButton];
    

	
	
	
	lastBack = YES;
	
	[self performSelector:@selector(loadText)];
}

-(void) loadText{
	NSString *descHtml = @"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
	"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
	"<head>"
	"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
	"</head>"
	"<title></title>"
	"<body style=\"padding:0px;margin:0px;width:100%;font-family:Sans-serif;\">"
	"<div style='font-size:20px;color:#333333;font-weight:bold;line-height:30px;margin-left:0;margin-right:0;margin-top:10px;position:relative;text-align:center'>%@</div>"
	"<div style='text-align:center;padding-bottom:8px;font-size:12px;margin-left:0;margin-right:0;position:relative;border-bottom:solid 1px #cccccc;color:#888888;'>%@&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;来自：%@</div>"
	"<div style='font-size:20px;color:#333333;line-height:30px;margin-left:0;margin-right:0;padding:6px 10px 6px 16px;position:relative;'>%@</div>"
	"</body>"
	"</html>";
	Rss *rss = [Rss shareRss];
    NSDictionary *dict = [rss.channels objectAtIndex:self.channelIndex];
	descHtml = [NSString stringWithFormat:descHtml,[item objectForKey:@"title"],
               [PublicMethods dateSwitchFromDateString:[item objectForKey:@"pubdate"] dateFormat:@"yyyy-MM-dd HH:mm:ss"] ,
                [dict objectForKey:@"Title"],
                [item objectForKey:@"description"]];
	[descWebView loadHTMLString:descHtml baseURL:nil];
}

-(void) newsDetail:(id)sender{
	descWebView.scalesPageToFit = YES;
    NSURL *url = [NSURL URLWithString:[item objectForKey:@"link"]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [descWebView loadRequest:request];
    lastBack = NO;
	moreButton.hidden = YES;
	descWebView.frame = CGRectMake(descWebView.frame.origin.x, descWebView.frame.origin.y, descWebView.frame.size.width, descWebView.frame.size.height + 44);
}

-(void) navBack:(id)sender{
	[self.navigationController popViewControllerAnimated:YES];
	UIApplication* app = [ UIApplication  sharedApplication ];  
	app.networkActivityIndicatorVisible = NO; 
}

-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSLog(@"%d",navigationType);
	if (navigationType==UIWebViewNavigationTypeOther) {
		lastBack = YES;
	}else {
		lastBack = NO;
	}
	return YES;
}

-(void) webViewDidStartLoad:(UIWebView *)webView{
	UIApplication* app = [ UIApplication  sharedApplication ];  
	app.networkActivityIndicatorVisible = YES; 
}

-(void) webViewDidFinishLoad:(UIWebView *)webView{
	UIApplication* app = [ UIApplication  sharedApplication ];  
	app.networkActivityIndicatorVisible = NO; 
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
	UIApplication* app = [ UIApplication  sharedApplication ];  
	app.networkActivityIndicatorVisible = NO; 
}


-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
	return (toInterfaceOrientation == UIInterfaceOrientationPortrait
			||toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown
			||toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
			||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}



- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
	if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationDuration:duration];
		if (moreButton.hidden) {
			descWebView.frame = CGRectMake(0, 34, 480, 300-34);
		}else {
			descWebView.frame = CGRectMake(0, 34, 480, 300-34-44);
		}
		navView.frame = CGRectMake(0, 0, 480, 37);
		moreButton.frame = CGRectMake(8, 300-40, 464, 33);
		[UIView commitAnimations];
	}else {
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationDuration:duration];
		if (moreButton.hidden) {
			descWebView.frame = CGRectMake(0, 34, 320, 460-34);
		}else {
			descWebView.frame = CGRectMake(0, 34, 320, 460-34-44);
		}
		navView.frame = CGRectMake(0, 0, 320, 37);
		moreButton.frame = CGRectMake(8, 460-40, 304, 33);
		[UIView commitAnimations];
	}
	
}

#pragma mark -
#pragma mark 手势
- (void)oneFingerSwipeRight:(UISwipeGestureRecognizer *)recognizer{ 
	[self performSelector:@selector(navBack:)];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	[item release];
}


- (void)dealloc {
    [super dealloc];
}


@end
